package com.yaojiaxun.logisticsPlatformSystem.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yaojiaxun.logisticsPlatformSystem.dto.MaterialInfoDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.MaterialInfo;
import com.yaojiaxun.logisticsPlatformSystem.entity.MaterialSearch;
import com.yaojiaxun.logisticsPlatformSystem.mapper.MaterialInfoMapper;
import com.yaojiaxun.logisticsPlatformSystem.util.SnowFlakeIDWorker;
import com.yaojiaxun.logisticsPlatformSystem.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: yjx_j
 * @Date: 2019/1/21 10:40
 * @Description:
 */
@Service
public class MaterialInfoService {

    @Autowired
    MaterialInfoMapper materialInfoMapper;

    public void insertMaterialInfo(MaterialInfo materialInfo){
        materialInfo.setMaterialId(new SnowFlakeIDWorker(3, 1).nextId());
        materialInfo.setCreateTime(Instant.now().toEpochMilli());
        materialInfo.setUpdateTime(Instant.now().toEpochMilli());
        materialInfoMapper.insertMaterialInfo(materialInfo);
    }

    public MaterialInfoDTO beanToDTO(MaterialInfo materialInfo){
        if (materialInfo == null)
            return null;
        MaterialInfoDTO materialInfoDTO = new MaterialInfoDTO();
        BeanUtils.copyProperties(materialInfo, materialInfoDTO);
        materialInfoDTO.setUpdateTime(TimeFormat.convertTime(materialInfo.getUpdateTime()));
        materialInfoDTO.setCreateTime(TimeFormat.convertTime(materialInfo.getCreateTime()));
        return materialInfoDTO;
    }

    public PageDTO<MaterialInfoDTO> findPage(Integer pageNum){
        PageDTO<MaterialInfoDTO> pageDTO = new PageDTO<>();
        Page<MaterialInfoDTO> page = new Page<>(pageNum, 10);
        List<MaterialInfoDTO> results = materialInfoMapper.selectPage(page, null).stream().map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setResult(results);
        pageDTO.setPage(page);
        return pageDTO;
    }

    public MaterialInfo findMaterialById(Long materialId){
        return materialInfoMapper.selectMaterialById(materialId);
    }

    public Boolean updateMaterialByMaterialId(MaterialInfo materialInfo){
        materialInfo.setUpdateTime(Instant.now().toEpochMilli());
        return materialInfoMapper.updateMaterialByMaterialId(materialInfo)>0;
    }

    public void deleteMaterial(Long materialId){
        materialInfoMapper.deleteMaterialById(materialId);
    }

    public List<MaterialInfo> findMaterialByCompany(String company){
        List<MaterialInfo> results = materialInfoMapper.findMaterialByCompany(company);
        return results;
    }

    public List<MaterialInfo> findMaterialByMaterialName(String materialName){
        List<MaterialInfo> results = materialInfoMapper.findMaterialByMaterialName(materialName);
        return results;
    }

    //通过供应商/客户或物资名称查找物资
    public List<MaterialInfo> findMatByCompanyOrMatName(String comOrName){
        List<MaterialInfo> results = new ArrayList<>();
        List<MaterialInfo> findByCom = findMaterialByCompany(comOrName);
        List<MaterialInfo> findByName = findMaterialByMaterialName(comOrName);
        results.addAll(findByCom);
        results.addAll(findByName);
        System.out.println(results);
        return results;
    }

    //取出后6条数据
    public List<MaterialInfoDTO> findLast6Material(){
        List<MaterialInfo> materials = materialInfoMapper.findLast6Material();
        List<MaterialInfoDTO> list = new ArrayList<>();
        for (int i = 0; i<materials.size(); i++){
            MaterialInfoDTO materialInfoDTO = beanToDTO(materials.get(i));
            materialInfoDTO.setUpdateTime(TimeFormat.convertTime(materials.get(i).getUpdateTime()));
            if (materials.get(i).getCapacity().equals("")){
                materialInfoDTO.setCapacity("不详");
            }
            if (materials.get(i).getWeight().equals("")){
                materialInfoDTO.setWeight("不详");
            }
            list.add(materialInfoDTO);
        }
        return list;
    }

    public MaterialInfoDTO findMaterialByIdFormatTime(Long materialId){
        MaterialInfo materialInfo = materialInfoMapper.selectMaterialById(materialId);
        MaterialInfoDTO materialInfoDTO = new MaterialInfoDTO();
        BeanUtils.copyProperties(materialInfo, materialInfoDTO);
        materialInfoDTO.setUpdateTime(TimeFormat.convertTime2(materialInfo.getUpdateTime()));
        if (materialInfo.getCapacity().equals("")){
            materialInfoDTO.setCapacity("不详");
        }
        if (materialInfo.getWeight().equals("")){
            materialInfoDTO.setWeight("不详");
        }
        return materialInfoDTO;
    }

    //根据出发地和到达地选择货源（带翻页功能）
    public PageDTO<MaterialInfoDTO> findPage(Integer pageNum, MaterialSearch materialSearch){
        PageDTO<MaterialInfoDTO> pageDTO = new PageDTO<>();
        Page<MaterialInfoDTO> page = new Page<>(pageNum, 10);
        List<MaterialInfoDTO> result = materialInfoMapper.selectPage(page, getWrapper(materialSearch)).stream().map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setResult(result);
        pageDTO.setPage(page);
        return pageDTO;
    }

    public Wrapper<MaterialInfo> getWrapper(MaterialSearch materialSearch){
        if (materialSearch == null)
            return null;
        Wrapper<MaterialInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("resource_area_province", materialSearch.getRProvince());
        wrapper.eq("resource_area_city", materialSearch.getRCity());
        wrapper.eq("resource_area_country", materialSearch.getRCountry());
        wrapper.eq("destination_area_province", materialSearch.getDProvince());
        wrapper.eq("destination_area_city", materialSearch.getDCity());
        wrapper.eq("destination_area_country", materialSearch.getDCountry());
        return wrapper;
    }
}
